<?php
namespace app\patrol\logic;
use app\patrol\validate\CpRegionModular as CpRegionModularValidate;
use app\patrol\validate\WorkStation as WorkStationValidate;

class PatrolLogic extends BaseLogic {

    /**
     * 获取楼层查询条件
     * @param $branchid
     * @return array
     */
    public function get_search_floor_maps($branchid){
        $maps = [];
        $maps[] = ['branchid','=',$branchid];
        return $maps;
    }

    /**
     * 检查开始巡店规则
     * @param $floorid
     * @return mixed
     */
    public function check_start_patrol_floor_rule($floorid){
        $maps = [];
        $maps[] = ['id','=',$floorid];
        $f_detail = model('floor')->get_detail($maps,'id');
        if(empty($f_detail)){
            return $this->get_response(0,'该巡店楼层不存在');
        }

        $data['f_detail'] = $f_detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 生成巡店楼层
     * @param $openid
     * @param $cp_type
     * @param $cp_branchid
     * @param $branchid
     * @param $floorid
     * @return mixed
     */
    public function gen_patrol_floor($openid,$cp_type,$cp_branchid,$branchid,$floorid){
        $maps = [];
        $maps[] = ['openid','=',$openid];
        $maps[] = ['cp_type','=',$cp_type];
        $maps[] = ['cp_branchid','=',$cp_branchid];
        $maps[] = ['branchid','=',$branchid];
        $maps[] = ['floorid','=',$floorid];
        $maps[] = ['status','<>',1];
        $cf_detail = model('cp_floor')->get_detail($maps,'id');
        if(!empty($cf_detail)){
            $cp_floorid = $cf_detail['id'];
        }else{
            $cf_data = [];
            $cf_data['openid'] = $openid;
            $cf_data['cp_type'] = $cp_type;
            $cf_data['cp_branchid'] = $cp_branchid;
            $cf_data['branchid'] = $branchid;
            $cf_data['floorid'] = $floorid;
            $cf_data['start_time'] = date('Y-m-d H:i:s');
            $cf_data['status'] = 2;
            $cp_floorid = model('cp_floor')->insertGetId($cf_data);
        }

        $data['cp_floorid'] = $cp_floorid;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 生成巡店区域和模块
     * @param $cp_type
     * @param $cp_branchid
     * @param $cp_floorid
     * @param $branchid
     * @param $floorid
     * @return mixed
     */
    public function gen_patrol_region_modular($cp_type,$cp_branchid,$cp_floorid,$branchid,$floorid){
        //生成社区巡店区域和模块
        $maps = [];
        $maps[] = ['floorid','=',$floorid];
        $r_list = model('region')->get_list($maps,'id,name,is_cp_ws');
        foreach($r_list as $k_1 => $v_1){
            $maps = [];
            $maps[] = ['regionid','=',$v_1['id']];
            $m_list = model('modular')->get_list($maps,'id,name');
            foreach($m_list as $k_2 => $v_2){
                $maps = [];
                $maps[] = ['cp_type','=',$cp_type];
                $maps[] = ['cp_branchid','=',$cp_branchid];
                $maps[] = ['cp_floorid','=',$cp_floorid];
                $maps[] = ['branchid','=',$branchid];
                $maps[] = ['floorid','=',$floorid];
                $maps[] = ['regionid','=',$v_1['id']];
                $maps[] = ['modularid','=',$v_2['id']];
                $crm_detail = model('cp_region_modular')->get_detail($maps);
                if(!empty($crm_detail)){

                }else{
                    $crm_data = [];
                    $crm_data['cp_type'] = $cp_type;
                    $crm_data['cp_branchid'] = $cp_branchid;
                    $crm_data['cp_floorid'] = $cp_floorid;
                    $crm_data['branchid'] = $branchid;
                    $crm_data['floorid'] = $floorid;
                    $crm_data['regionid'] = $v_1['id'];
                    $crm_data['modularid'] = $v_2['id'];
                    $r = model('cp_region_modular')->insert($crm_data);
                }
            }
        }
        return $this->get_response(1,'OK');
    }

    /**
     * 获取巡店区域和模块
     * @param $openid
     * @param $cp_type
     * @param $cp_floorid
     * @return mixed
     */
    public function get_patrol_region_modular($openid,$cp_type,$cp_floorid){
        $maps = [];
        $maps[] = ['id','=',$cp_floorid];
        $maps[] = ['openid','=',$openid];
        $maps[] = ['cp_type','=',$cp_type];
        $cf_detail = model('cp_floor')->get_detail($maps,'id,cp_branchid,branchid,floorid');
        if(empty($cf_detail)){
            return $this->get_response(0,'系统异常，请联系管理员');
        }

        //查找区域
        $maps = [];
        $maps[] = ['floorid','=',$cf_detail['floorid']];
        $r_list = model('region')->get_list($maps,'id,name,is_cp_ws');
        $list = [];
        foreach($r_list as $k_1 => $v_1){
            $list[$v_1['id']] = $v_1->toArray();

            $maps = [];
            $maps[] = ['regionid','=',$v_1['id']];
            $m_list = model('modular')->get_list($maps,'id,name');
            $temp_list = [];
            foreach($m_list as $k_2 => $v_2){
                $temp_list[$v_2['id']] = $v_2->toArray();

                $maps = [];
                $maps[] = ['cp_type','=',$cp_type];
                $maps[] = ['cp_branchid','=',$cf_detail['cp_branchid']];
                $maps[] = ['cp_floorid','=',$cf_detail['id']];
                $maps[] = ['branchid','=',$cf_detail['branchid']];
                $maps[] = ['floorid','=',$cf_detail['floorid']];
                $maps[] = ['regionid','=',$v_1['id']];
                $maps[] = ['modularid','=',$v_2['id']];
                $crm_detail = model('cp_region_modular')->get_detail($maps);
                $crm_detail['pic'] = !empty($crm_detail['pic']) ? json_decode($crm_detail['pic'],true) : [];

                //查找事项
                $maps = [];
                $maps[] = ['from_openid','=',$openid];
                $maps[] = ['sourceid','=',$crm_detail['id']];
                $maps[] = ['source','=',1];
                $m_detail = model('matter')->get_detail($maps,'id,out_time,deal_openid');

                $crm_detail['out_time'] = !empty($m_detail) ? $m_detail['out_time'] : '';
                $crm_detail['out_time_hi'] = !empty($m_detail) ? date('H:i',strtotime($m_detail['out_time'])) : '';
                $crm_detail['deal_openid'] = !empty($m_detail) ? $m_detail['deal_openid'] : '';

                $maps = [];
                $maps[] = ['openid','=',$crm_detail['deal_openid']];
                $u_detail = model('user')->get_detail($maps,'id,username');
                $crm_detail['deal_username'] = !empty($u_detail) ? $u_detail['username'] : '';

                //查找抄送
                $maps = [];
                $maps[] = ['from_openid','=',$openid];
                $maps[] = ['sourceid','=',$crm_detail['id']];
                $maps[] = ['source','=',1];
                $copy_openid_arr = model('copy')->get_column($maps,'copy_openid');

                $crm_detail['copy_openid'] = $copy_openid_arr;

                $maps = [];
                $maps[] = ['openid','in',$crm_detail['copy_openid']];
                $copy_username_arr = model('user')->get_column($maps,'username');
                $crm_detail['copy_username'] = $copy_username_arr;

                $temp_list[$v_2['id']]['crm_detail'] = $crm_detail;
            }
            $list[$v_1['id']]['m_list'] = $temp_list;
        }

        $data['list'] = $list;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 检查更新巡店区域和模块规则
     * @param $cp_type
     * @param $params
     * @return mixed
     */
    public function check_save_patrol_region_modular_rule($cp_type,$params){
        $validate = new CpRegionModularValidate();
        $r = $validate->scene('add')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        //检查该区域和模块是否巡店完成
        $maps = [];
        $maps[] = ['id','=',$params['crmid']];
        $crm_detail = model('cp_region_modular')->get_detail($maps,'id,cp_branchid,cp_floorid,branchid,floorid,regionid');
        if(empty($crm_detail)){
            return $this->get_response(0,'系统异常，请联系管理员');
        }

        //检查楼层是否巡店完成
        $maps = [];
        $maps[] = ['id','=',$crm_detail['cp_floorid']];
        $cf_detail = model('cp_floor')->get_detail($maps,'id,status');
        if($cf_detail['status'] == 1){
            return $this->get_response(0,'该楼层您已完成巡店');
        }

        if($params['action'] == 2){
            if(empty($params['out_time'])){
                return $this->get_response(0,'处理时间必须填写');
            }
        }

        if($params['action'] == 3){
            if(empty($params['deal_openid'])){
                return $this->get_response(0,'移交对象必须填写');
            }
        }

        //检查该区域是否巡店工位
        $maps = [];
        $maps[] = ['id','=',$crm_detail['regionid']];
        $r_detail = model('region')->get_detail($maps,'id,is_cp_ws');
        if(empty($r_detail)){
            return $this->get_response(0,'系统异常，请联系管理员');
        }
        if($r_detail['is_cp_ws'] == 1 && $cp_type == 1){
            //查找已巡店工位
            $maps = [];
            $maps[] = ['cp_type','=',$cp_type];
            $maps[] = ['cp_branchid','=',$crm_detail['cp_branchid']];
            $maps[] = ['cp_floorid','=',$crm_detail['cp_floorid']];
            $maps[] = ['branchid','=',$crm_detail['branchid']];
            $maps[] = ['floorid','=',$crm_detail['floorid']];
            $maps[] = ['regionid','=',$r_detail['id']];
            $maps[] = ['status','=',1];
            $cp_wsids = model('cp_region_ws')->get_column($maps,'wsid');

            //查找原本应该巡店工位
            $maps = [];
            $maps[] = ['regionid','=',$r_detail['id']];
            $wsids = model('work_station')->get_column($maps,'id');

            //求两个数组的差集
            $diff_arr = array_diff($wsids,$cp_wsids);
            if(empty($cp_wsids) || empty($wsids) || !empty($diff_arr)){
                return $this->get_response(0,'该区域您还未巡店工位');
            }
        }

        $data['crm_detail'] = $crm_detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 更新巡店区域和模块
     * @param $openid
     * @param $params
     * @param $crm_detail
     * @return mixed
     */
    public function save_patrol_region_modular($openid,$params,$crm_detail){
        $crm_data = [];
        $crm_data['is_qualified'] = $params['is_qualified'];
        $crm_data['describe'] = trim($params['describe']);
        $crm_data['pic'] = json_encode($params['pic'],JSON_UNESCAPED_UNICODE);
        $crm_data['action'] = $params['action'];
        $crm_data['explain'] = $params['explain'];
        $crm_data['status'] = 1;

        $maps = [];
        $maps[] = ['id','=',$crm_detail['id']];
        $r = model('cp_region_modular')->force()->save($crm_data,$maps);
        if($r !== false){
            //生成事项（未开始）
            $m = new \app\common\controller\Matter();
            if($params['action'] == 2){
                $r = $m->gen_matter_not($openid,$crm_detail['id'],1,$params['out_time'],$openid,1);
            }elseif($params['action'] == 3){
                $out_time = date('Y-m-d H:i:s',strtotime('+12 hour'));
                //$out_time = '0000-00-00 00:00:00';
                $r = $m->gen_matter_not($openid,$crm_detail['id'],1,$out_time,$params['deal_openid'],0);
            }

            //生成抄送（未抄送）
            $c = new \app\common\controller\Copy();
            //if(!empty($params['copy_openid'])){
                $r = $c->gen_copy_not($openid,$crm_detail['id'],1,$params['copy_openid']);
            //}
            return $this->get_response(1,'OK');
        }
        return $this->get_response(0,'更新社区巡店区域和模块失败');
    }

    /**
     * 获取工位查询条件
     * @param $regionid
     * @return array
     */
    public function get_search_ws_maps($regionid){
        $maps = [];
        $maps[] = ['regionid','=',$regionid];

        return $maps;
    }

    /**
     * 检查修改工位规则
     * @param $params
     * @return mixed
     */
    public function check_save_ws_rule($params){
        $validate = new WorkStationValidate();
        $r = $validate->scene('edit_ws')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        $maps = [];
        $maps[] = ['id','=',$params['wsid']];
        $ws_detail = model('work_station')->get_detail($maps,'id');
        if(empty($ws_detail)){
            return $this->get_response(0,'该工位不存在');
        }

        $data['ws_detail'] = $ws_detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 检查修改公司规则
     * @param $params
     * @return mixed
     */
    public function check_save_company_rule($params){
        $validate = new WorkStationValidate();
        $r = $validate->scene('edit_company')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        $maps = [];
        $maps[] = ['id','=',$params['wsid']];
        $ws_detail = model('work_station')->get_detail($maps,'id');
        if(empty($ws_detail)){
            return $this->get_response(0,'该工位不存在');
        }

        $data['ws_detail'] = $ws_detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 获取沟通备注查询条件
     * @param $wsid
     * @return array
     */
    public function get_search_remark_maps($wsid){
        $maps = [];
        $maps[] = ['remark','<>',''];
        $maps[] = ['wsid','=',$wsid];

        return $maps;
    }
}